package com.shrio;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by luojie on 2018/3/9.
 */
public class PC {
    public static void main(String[] args) {
        Store store = new Store();
        Provider provider = new Provider(store);
        Comsumer comsumer = new Comsumer(store);
        Thread thread = new Thread(provider);
        Thread thread1 = new Thread(comsumer);
        thread.start();
        thread1.start();
    }
}

class Store{

    private int maxSite;
    private List<Date> list;

    public Store() {
        this.maxSite = 10;
        this.list = new LinkedList<Date>();
    }

    public synchronized void set() {
        if(list.size() == maxSite){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        list.add(new Date());
        System.out.println("----set-----"+list.size());
        notifyAll();
    }

    public synchronized void get(){
        if(list.size() ==0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Date date =(Date) ((LinkedList) list).pop();
        System.out.println("-------get--------"+list.size());
        notifyAll();
    }
}

class Provider implements Runnable{

    private Store store;

    public Provider(Store store){
        this.store = store;
    }

    @Override
    public void run() {
        store.set();
    }
}

class Comsumer implements Runnable{

    private Store store;

    public Comsumer(Store store){
        this.store = store;
    }

    @Override
    public void run() {
        store.get();
    }
}